package requestManager;

import java.io.File;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import resources.ResourceProvider;

import model.Acknowledgement;
import model.EmailImp;
import model.MeetingInfo;
import model.MeetingMail;

/**
 * Message Receive Callback
 */
public class MessageReceivedCallback implements Observer
{
    
    private EmailApplicationRequestManagerImp emailApplicationRequestManager;
    
    /**
     * Constructor
     * @param emailApplicationRequestManager the reference of the email application request manager
     */
    public MessageReceivedCallback(EmailApplicationRequestManagerImp emailApplicationRequestManager)
    {
        this.emailApplicationRequestManager = emailApplicationRequestManager;
    }
   
    /*
     * (non-Javadoc)
     * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
     */
    public void update(Observable o, Object receivedObject)
    {
           if(receivedObject instanceof EmailImp)
            {
                EmailImp email = (EmailImp) receivedObject;
                String persistPath = ResourceProvider.getRootFolderPath(ResourceProvider.INBOX, emailApplicationRequestManager.getUser());
                this.emailApplicationRequestManager.requestPersitEmail(email, persistPath);
                
                if(email instanceof MeetingMail)
                {
                    MeetingMail meetingMail = (MeetingMail) email;
                    String meetingID = meetingMail.getMeetingId();
                    String user = this.emailApplicationRequestManager.getUser();
                    String meetingPath = ResourceProvider.getRootFolderPath(ResourceProvider.MEETING, user);
                    String meetingDataPath = meetingPath + ResourceProvider.getFilePathSeperator() + meetingID + ".json";
                    //check if meetingID is already have meeting info
                    if(!new File(meetingDataPath).exists())
                    {
                        //creating for the first time
                        MeetingInfo info = new MeetingInfo(meetingID);
                        info.setOrigianlPlace(meetingMail.getMeetingPlace());
                        info.setOriginalMessage(meetingMail.getMessage());
                        info.setOriginalStartTime(meetingMail.getStartTime());
                        info.setOriginalEndiTime(meetingMail.getEndTime());
                        info.setOriginalSubject(meetingMail.getSubject());
                        info.setReceivers(meetingMail.getReceiver());
                        info.setSender(meetingMail.getSender());
                        this.emailApplicationRequestManager.requestPersistMeetingInfo(info, meetingPath);
                    } 
                }    
            } 
           else if(receivedObject instanceof Acknowledgement)
           {
        	   Acknowledgement ack = (Acknowledgement) receivedObject;
        	   
        	   if(ack.action == Acknowledgement.ACK_EMAIL_SENT_SUCCESSFULLY)
        	   {
                   String emailPathInOutbox = (String) ack.receivedObj;
                   
                   String sentPath = ResourceProvider.getRootFolderPath(ResourceProvider.SENT, emailApplicationRequestManager.getUser());
                   EmailImp email = this.emailApplicationRequestManager.requestEmailFromJson(emailPathInOutbox);
                   this.emailApplicationRequestManager.requestFileDelete(emailPathInOutbox);
                   this.emailApplicationRequestManager.requestPersitEmail(email, sentPath);
        	   }
        	   else if(ack.action == Acknowledgement.ACK_MEETING_ACCEPTED)
               {
                   String meetingID = (String) ack.receivedObj;
                   String user = this.emailApplicationRequestManager.getUser();

                   String meetingIDPath = ResourceProvider.getRootFolderPath(ResourceProvider.MEETING, user) + ResourceProvider.getFilePathSeperator() + meetingID +".json";
                   
                   MeetingInfo meetingInfo = this.emailApplicationRequestManager.requestMeetingInfo(meetingIDPath);
                   Map<Integer, String> data = meetingInfo.meetingParticipantData;
                   data.remove(ack.from.hashCode());
                   data.put(ack.from.hashCode(), MeetingInfo.MEETING_ACCEPTED);
                   
                   String meetingPath = ResourceProvider.getRootFolderPath(ResourceProvider.MEETING, user);
                   
                   this.emailApplicationRequestManager.requestPersistMeetingInfo(meetingInfo, meetingPath);
               }
        	   else if(ack.action == Acknowledgement.ACK_MEETING_REJECTED)
               {
        	       String meetingID = (String) ack.receivedObj;
                   String user = this.emailApplicationRequestManager.getUser();

                   String meetingIDPath = ResourceProvider.getRootFolderPath(ResourceProvider.MEETING, user) + ResourceProvider.getFilePathSeperator() + meetingID +".json";
                   
                   MeetingInfo meetingInfo = this.emailApplicationRequestManager.requestMeetingInfo(meetingIDPath);
                   Map<Integer, String> data = meetingInfo.meetingParticipantData;
                   data.remove(ack.from.hashCode());
                   data.put(ack.from.hashCode(), MeetingInfo.MEETING_REJECTED);
                   
                   String meetingPath = ResourceProvider.getRootFolderPath(ResourceProvider.MEETING, user);
                   
                   this.emailApplicationRequestManager.requestPersistMeetingInfo(meetingInfo, meetingPath);
               }
        	   else if(ack.action == Acknowledgement.ACK_MEETING_UPDATED_TO_PERSIST)
               {                 
        	       MeetingInfo meetingInfo = (MeetingInfo) ack.receivedObj;
                   String user = this.emailApplicationRequestManager.getUser();
                   String meetingID = meetingInfo.meetingID;                   
                   String meetingIDPath = ResourceProvider.getRootFolderPath(ResourceProvider.MEETING, user) + ResourceProvider.getFilePathSeperator() + meetingID +".json";

                   MeetingInfo meeting = this.emailApplicationRequestManager.requestMeetingInfo(meetingIDPath);

                           meeting.setOrigianlPlace(meetingInfo.origianlPlace);
                           meeting.setOriginalEndiTime(meetingInfo.originalEndiTime);
                           meeting.setOriginalMessage(meetingInfo.originalMessage);
                           meeting.setOriginalStartTime(meetingInfo.originalStartTime);
                           meeting.setOriginalSubject(meetingInfo.originalSubject);

                           String meetingPath = ResourceProvider.getRootFolderPath(ResourceProvider.MEETING, user);
                           
                           this.emailApplicationRequestManager.requestPersistMeetingInfo(meeting, meetingPath);     	       
        	  }
        	  else if(ack.action == Acknowledgement.ACK_MEETING_CANCELLED)
              {
        	      String meetingID = (String) ack.receivedObj;
                  String user = this.emailApplicationRequestManager.getUser();

                  String meetingIDPath = ResourceProvider.getRootFolderPath(ResourceProvider.MEETING, user) + ResourceProvider.getFilePathSeperator() + meetingID +".json";
                  
                  MeetingInfo meetingInfo = this.emailApplicationRequestManager.requestMeetingInfo(meetingIDPath);
                  
                  Map<Integer, String> data = meetingInfo.meetingParticipantData;
                  for(Integer userId : data.keySet())
                  {
                      data.put(userId, MeetingInfo.MEETING_CANCELLED);
                  }
                  
                  String meetingPath = ResourceProvider.getRootFolderPath(ResourceProvider.MEETING, user);
                  
                  this.emailApplicationRequestManager.requestPersistMeetingInfo(meetingInfo, meetingPath);
              }	   
        } 
    }
}
